<?php
// 方弦研究所
namespace app;

/**
 * 首页类
 */
class Index extends Init
{
    /**
     * 首页
     * @return void
     */
    public function index()
    {
        // 初始化变量
        $config = \fxapp\Base::config('env.base');
        $tray['file'] = $config['root'] . 'static' . DIRECTORY_SEPARATOR . 'obj_collage.inc.php';
        $tray['content'] = file_get_contents($tray['file']);
        // $tray['content'] = substr($tray['content'], 0, 200);
        $tray['data'] = $tray['content'];
        unset($tray['content']);
        // 文字编码转换
        while (true) {
            $tray['pattern'] = '\\\\[x0-9][a-z0-9]{2}';
            $tray['result'] = preg_match('/' . $tray['pattern'] . '/', $tray['data'], $tray['value']);
            if ($tray['result'] === 0) break;
            $tray['value'] = $tray['value'][0];
            if (strpos($tray['value'], '\\x') === 0) {
                // 转换16进制
                // $tray['value'] = substr($tray['value'], 2);
                // $tray['value'] = hex2bin($tray['value']);
                $tray['value'] = substr($tray['value'], 2);
                $tray['value'] = chr(hexdec($tray['value']));
            } else {
                // 转换8进制
                // $error['8'] = $tray['value'];
                // $tray['value'] = base_convert(substr($tray['value'], 1), 8, 16);
                // $error['16'] = $tray['value'];
                // if (strlen($tray['value']) > 2) {
                //     var_dump($error);
                // }
                // $tray['value'] = hex2bin($tray['value']);
                $tray['value'] = substr($tray['value'], 1);
                $tray['value'] = chr(octdec($tray['value']));
            }
            $tray['data'] = preg_replace('/' . $tray['pattern'] . '/', $tray['value'], $tray['data'], 1);
        }
        while (true) {
            $tray['pattern'] = '\\\\[a-z0-9]{2}';
            $tray['result'] = preg_match('/' . $tray['pattern'] . '/', $tray['data'], $tray['value']);
            if ($tray['result'] === 0) break;
            $tray['value'] = $tray['value'][0];
            // 转换8进制
            // $error['8'] = $tray['value'];
            // $tray['value'] = base_convert(substr($tray['value'], 1), 8, 16);
            // $error['16'] = $tray['value'];
            // if (strlen($tray['value']) > 2) {
            //     var_dump($error);
            // }
            // $tray['value'] = hex2bin($tray['value']);
            $tray['value'] = substr($tray['value'], 1);
            $tray['value'] = chr(octdec($tray['value']));
            $tray['data'] = preg_replace('/' . $tray['pattern'] . '/', $tray['value'], $tray['data'], 1);
        }
        // $tray['pattern'] = 'goto [\S]{5};';
        // $tray['result'] = preg_match_all('/' . $tray['pattern'] . '/', $tray['data'], $tray['value']);
        $file['file'] = $config['root'] . 'static' . DIRECTORY_SEPARATOR . 'obj_collage.inc.parse.php';
        $file['base'] = fopen($file['file'], "w");
        $file['content'] = $tray['data'];
        fwrite($file['base'], $file['content']);
        fclose($file['base']);
        var_dump($tray);
    }

    /**
     * 测试
     * @return void
     */
    public function test()
    {
        // 初始化变量
        $config = \fxapp\Base::config('env.base');
        include $config['root'] . 'static' . DIRECTORY_SEPARATOR . 'test.php';
    }

    /**
     * 文本
     * @return void
     */
    public function text()
    {
        // 初始化变量
        $tray['content'] = "\x20\x77\x68\145\162\145\40\141\166\x61\164\141\162\74\76\42\42\x20\x6f\162\144\145\x72\40\x62\x79\x20\x75\x69\x64\40\x64\145\x73\143\x20\114\x49\x4d\111\x54\40\61\60\x2c\64\61";
        echo $tray['content'], PHP_EOL;
        // 初始化变量
        $config = \fxapp\Base::config('env.base');
        $tray['file'] = $config['root'] . 'static' . DIRECTORY_SEPARATOR . 'obj_collage.inc.php';
        $tray['content'] = file_get_contents($tray['file']);
        // $tray['content'] = substr($tray['content'], 0, 200);
        $tray['data'] = $tray['content'];
        // echo $tray['data'], PHP_EOL;
        // unset($tray['content']);
        // 转存文字文件
        $tray['list'] = [];
        while (true) {
            $tray['pattern'] = '((\\\\[x0-9][a-z0-9]{1,2})+)';
            $tray['result'] = preg_match('/' . $tray['pattern'] . '/', $tray['data'], $tray['value']);
            if ($tray['result'] === 0) break;
            $tray['value'] = $tray['value'][0];
            $tray['list'][] = $tray['value'];
            $tray['data'] = preg_replace('/' . $tray['pattern'] . '/', '', $tray['data'], 1);
        }
        $file['file'] = $config['root'] . 'static' . DIRECTORY_SEPARATOR . 'obj_collage.inc.text.php';
        $file['base'] = fopen($file['file'], "w");
        $elem = '<?php' . PHP_EOL;
        fwrite($file['base'], $elem);
        foreach ($tray['list'] as $key => $value) {
            if (count($tray['list']) != $key + 1) {
                $elem = ',';
            } else {
                $elem = '';
            }
            $elem = 'echo ' . \fxapp\Param::json($value, 'encode') . ', "' . $value . '", PHP_EOL;' . PHP_EOL;
            fwrite($file['base'], $elem);
        }
        fclose($file['base']);
        var_dump(date('Y-m-d H:i:s') . '：写入文件');
        // 替换文字
        $tray['data'] = $tray['content'];
        $tray['path'] = $config['web'] . '/static/obj_collage.inc.text.php';
        $tray['format'] = file_get_contents($tray['path']);
        $tray['format'] = explode(PHP_EOL, $tray['format']);
        foreach ($tray['format'] as $key => $value) {
            $elem = [];
            $tray['pattern'] = '((\\\\[x0-9][a-z0-9]{1,2})+)';
            $tray['result'] = preg_match('/' . $tray['pattern'] . '/', $value, $tray['value']);
            if ($tray['result'] === 0) continue;
            $tray['value'] = $tray['value'][0];
            // var_dump($tray['value']);
            $tray['value'] = substr($value, strlen($tray['value']));
            // var_dump($tray['value']);
            $tray['value'] = str_replace('"', '\"', $tray['value']);
            $tray['data'] = preg_replace('/' . $tray['pattern'] . '/', $tray['value'], $tray['data'], 1);
        }
        // 写入解析文件
        $file['file'] = $config['root'] . 'static' . DIRECTORY_SEPARATOR . 'obj_collage.inc.parse.php';
        $file['base'] = fopen($file['file'], "w");
        $file['content'] = $tray['data'];
        fwrite($file['base'], $file['content']);
        fclose($file['base']);
        // var_dump($tray['data']);
        // var_dump($tray['list']);
    }

    /**
     * 文本2
     * @return void
     */
    public function text2()
    {
        // 初始化变量
        $tray['content'] = "\x20\x77\x68\145\162\145\40\141\166\x61\164\141\162\74\76\42\42\x20\x6f\162\144\145\x72\40\x62\x79\x20\x75\x69\x64\40\x64\145\x73\143\x20\114\x49\x4d\111\x54\40\61\60\x2c\64\61";
        echo $tray['content'], PHP_EOL;
        // 初始化变量
        $config = \fxapp\Base::config('env.base');
        $tray['file'] = $config['root'] . 'static' . DIRECTORY_SEPARATOR . 'obj_collage.inc.php';
        $tray['content'] = file_get_contents($tray['file']);
        // $tray['content'] = substr($tray['content'], 0, 200);
        $tray['data'] = $tray['content'];
        // echo $tray['data'], PHP_EOL;
        unset($tray['content']);
        // 转存文字文件
        $tray['list'] = [];
        while (true) {
            $tray['pattern'] = '((\\\\[x0-9][a-z0-9]{1,2})+)';
            $tray['result'] = preg_match('/' . $tray['pattern'] . '/', $tray['data'], $tray['value']);
            // var_dump($tray['value']);
            if ($tray['result'] === 0) break;
            $tray['value'] = $tray['value'][0];
            // $tray['list'][] = substr($tray['value'], 1, -1);
            $tray['list'][] = $tray['value'];
            // if (count($tray['list']) > 100) break;
            $tray['data'] = preg_replace('/' . $tray['pattern'] . '/', '', $tray['data'], 1);
        }
        $file['file'] = $config['root'] . 'static' . DIRECTORY_SEPARATOR . 'obj_collage.inc.text.php';
        $file['base'] = fopen($file['file'], "w");
        $elem = '<?php' . PHP_EOL;
        fwrite($file['base'], $elem);
        $elem = 'echo "{", PHP_EOL;' . PHP_EOL;
        fwrite($file['base'], $elem);
        foreach ($tray['list'] as $key => $value) {
            if (count($tray['list']) != $key + 1) {
                $elem = ',';
            } else {
                $elem = '';
            }
            $elem = 'echo ' . \fxapp\Param::json(\fxapp\Param::json($value, 'encode'), 'encode') . ', ":", "\"' . $value . '\"' . $elem . '", PHP_EOL;' . PHP_EOL;
            fwrite($file['base'], $elem);
        }
        $elem = 'echo "}", PHP_EOL;' . PHP_EOL;
        fwrite($file['base'], $elem);
        fclose($file['base']);
        var_dump(date('Y-m-d H:i:s') . '：写入文件');
        // var_dump($tray['list']);
    }
}
